package display {
    import com.adobe.serialization.json.JSON;
    
    import flare.animate.Sequence;
    import flare.animate.Transitioner;
    import flare.animate.Tween;
    import flare.data.DataSet;
    import flare.data.converters.GraphMLConverter;
    import flare.flex.FlareVis;
    import flare.vis.Visualization;
    import flare.vis.data.Data;
    import flare.vis.data.EdgeSprite;
    import flare.vis.data.NodeSprite;
    
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    
    import mx.controls.List;
    
    import org.aswing.AsWingManager;
    import org.aswing.Container;
    import org.aswing.GridLayout;
    import org.aswing.JFrame;
    import org.aswing.JLabel;
    import org.aswing.JList;
    import org.aswing.JPanel;
    import org.aswing.JScrollPane;
    import org.aswing.JWindow;
    import org.aswing.ListModel;
    import org.aswing.LoadIcon;
    import org.aswing.VectorListModel;
    import org.aswing.event.SelectionEvent;
    import org.aswing.ext.GeneralGridListCellFactory;
    import org.aswing.ext.GridList;
    
    import util.IconGridCell;
    import util.IconListCellFactory;
    
    public class FriendRecommendationSprite extends Sprite {
        public var panel:JFrame;
        public var photos:GridList;
        private var _vis:Visualization;
        public var data:Data;
        public function FriendRecommendationSprite(stage:Stage, vis:Visualization, recommendation:String) {
            super();
            AsWingManager.setRoot(stage);
            panel = new JFrame(null, "Recommended Friends");
            panel.setClosable(false);
            panel.setDragable(false);
            panel.setResizable(false);
            
            photos = new GridList(null, new GeneralGridListCellFactory(IconGridCell), 5, 2);
            photos.addSelectionListener(onSelect);
            var pane:JScrollPane = new JScrollPane();
            pane.setView(photos);
            panel.getContentPane().append(pane);
            
            panel.setSizeWH(230, 86);
            panel.setX(0);
            panel.setY(20);
            
            _vis = vis;
            
            loadData(recommendation);
            //          new SelectionEvent
        }
        private function loadData(file:String):void {
            var loader:URLLoader = new URLLoader();
            var req:URLRequest = new URLRequest(file);
            loader.addEventListener(Event.COMPLETE, function(event:Event):void {
                var l:URLLoader = event.target as URLLoader;
                // var dataSet:DataSet = new DataSetProtoMLConverter().parse(new XML(l.data));
                // trace('loadData >' + l.data);
                var dataset:DataSet = new GraphMLConverter().parse(new XML(l.data));
                data = Data.fromDataSet(dataset);
//                var friends:Array = [];
//                for each (var node:NodeSprite in data.nodes) {
//                    trace(' >' + JSON.encode(node.data));
//                    friends.push(node.data);
//                }
                trace(JSON.encode(data.nodes.toDataArray()));
                photos.setListData(data.nodes.toDataArray());
                panel.show();
            });
            loader.load(req);
        }
        public function onSelect(event:SelectionEvent):void {
//            trace(' onSelect >');
            var t:GridList = event.currentTarget as GridList;
            var l:Number = event.getLastIndex();
            var m:VectorListModel = t.getModel() as VectorListModel;
//            data.nodes.
                var a:Array = data.nodes.toDataArray();
                trace('a >', a.length, JSON.encode(a), l);
                trace('onSelect >', JSON.encode(a[l]));
            var d:Object = m.getElementAt(l);
            trace('onSelect >', JSON.encode(d));
            var r:NodeSprite = _vis.data.nodes[int(Math.random() * _vis.data.nodes.length)];
//            for each (var node:NodeSprite in _vis.data.nodes) {
//                if (node.data.self == true) {
//                    r = node;
//                }
//            }
//            if (!r) r = _vis.data.nodes[0];
            if (!r) throw new Error('no root.');
            var n:NodeSprite = _vis.data.addNode(d);
            n.dirty();
            trace('root >', r);

            var tuple:Object = {
                'service_type': 0,
                'group': 'contact',
                'open_public': true,
                'activity': 0,
                'distance': 40
            };

            var edge:EdgeSprite = _vis.data.addEdgeFor(n, r, null, tuple);
            edge.visible = false;
            edge.alpha = 0;
            edge.dirty();

            var tr:Transitioner = _vis.update(1.0, "main", "nodes", "edges", "layout");
            tr.play();

        }
        //	  public function update(friends:Array):void {
        //
        //		  var list:Array = [];
        //		  for (var i:int = 0; i < friends.length; ++i) {
        //			  list.push(friends[i]);
        //			  trace('FriendRecommendationSprite >' + friends[i]);
        //
        //		  }
        
        //	  }
    }
}
